﻿import QtQuick 2.2
import Box2D 2.0
import "../shared"
import QtQuick.Controls 1.4


Item {
    id: screen

    property int hz: 60

    Component {
        id: linkComponent

        PhysicsItem {
            id: link

            width: 20
            height: 20
            z: 1
            bodyType: Body.Dynamic

            fixtures: Box {
                width: link.width
                height: link.height
                density: 0.2
                friction: 0.3
            }

            Rectangle {
                anchors.fill: parent
                border.color: "#DEDEDE"
                color: "#44A51C"
            }
        }
    }

    Component {
        id: linkJoint
        WeldJoint {
            dampingRatio: 0.5
            frequencyHz: hz
        }
    }

    function createBranch(base,count) {
        var prevLink = base;
        var angle = Math.random() * 10 - 5;
        for (var i = 0; i < count; i++) {
            var newLink = linkComponent.createObject(screen);
            newLink.width = 20;
            newLink.height = 3 + count - i;
            if (count % 2) {
                newLink.x = prevLink.x + prevLink.width;
                newLink.y = prevLink.y - (prevLink.height / 2);
            } else {
                newLink.x = prevLink.x - newLink.width;
                newLink.y = prevLink.y - (prevLink.height / 2);
            }
            var newJoint = linkJoint.createObject(screen);
            if (count % 2) {
                newJoint.localAnchorA = Qt.point(prevLink.width, prevLink.height / 2);
                newJoint.localAnchorB = Qt.point(0, newLink.height / 2);
            } else {
                newJoint.localAnchorA = Qt.point(0, prevLink.height / 2);
                newJoint.localAnchorB = Qt.point(newLink.width, newLink.height / 2);
            }
            newJoint.referenceAngle = angle;
            newJoint.bodyA = prevLink.body;
            newJoint.bodyB = newLink.body;
            prevLink = newLink;
        }
    }

    World { id: physicsWorld }

    Component.onCompleted: {
        var prevLink = bodyA;
        for (var i = 0; i < 10; i++) {
            var newLink = linkComponent.createObject(screen);
            newLink.x = 205 + i * 3;
            newLink.y = 400 - (i * 40);
            newLink.width = 20 - i * 1.5;
            newLink.height = 40;
            var newJoint = linkJoint.createObject(screen);
            if (i == 0)
                newJoint.localAnchorA = Qt.point(bodyA.width / 2, 0);
            else
                newJoint.localAnchorA = Qt.point(newLink.width / 2, 0);
            newJoint.localAnchorB = Qt.point(newLink.width / 2, newLink.height);
            newJoint.bodyA = prevLink.body;
            newJoint.bodyB = newLink.body;
            prevLink = newLink;
            createBranch(newLink, 10 - i);
        }
    }

    PhysicsItem {
        id: bodyA
        width: 100
        height: 20
        x: 160
        y: 400
        fixtures: Box {
            width: bodyA.width
            height: bodyA.height
        }
        Rectangle {
            anchors.fill: parent
            color: "#44A51C"
        }
    }

//    PhysicsItem {
//        id: wenzi1
//        x: 160
//        y: 160

//        onYChanged: {
//            if(y > parent.height){
//                y = 0
//            }
//        }

//        width: 80
//        height: 30
//        z: 1
//        bodyType: Body.Dynamic

//        fixtures: Box {
//            width: wenzi1.width
//            height: wenzi1.height
//            density: 0.1
//            friction: 0.5
//        }

//        Text {
//            id: name
//            text: qsTr("刘芷吟")
//            font.pixelSize: 30
//            color: "pink"
//        }
//    }

//    PhysicsItem {
//        id: wenzi2
//        x: 225
//        y: 236
//        onYChanged: {
//            if(y > parent.height){
//                y = 0
//            }
//        }
//        width: 50
//        height: 30
//        z: 1
//        bodyType: Body.Dynamic

//        fixtures: Box {
//            width: wenzi2.width
//            height: wenzi2.height
//            density: 0.1
//            friction: 0.5
//        }

//        Text {
//            id: name2
//            text: qsTr("陈超")
//            font.pixelSize: 30
//            color: "yellow"
//        }
//    }


    DebugDraw {
        id: debugDraw
        world: physicsWorld
        opacity: 1
        visible: false
        z: 10
    }
}
